// 部署过程中，对服务进行优雅上下线，实现用户无感知部署
def isParamsInProjectName = currentBuild.projectName.endsWith(")")
def nodeName = isParamsInProjectName ? currentBuild.projectName.find(/\(([^\)]+)\)/, {group -> group[1]}) : params.NODE_NAME
def projectName = currentBuild.projectName.split('\\(')[0]
if (params.ENV != null) {
    projectName = projectName.replace("mdm", "mdm-${params.ENV}")
}

println("nodeName: ${nodeName}, projectName: ${projectName}, params: ${params}, env: ${env}")
def buildProjectName = isParamsInProjectName ? "mdm-java-server-build(159test)" : "mdm-java-server-build"

def allEnvProps = [
    'rbm-pilotrun-console': [
        buildProjectName: buildProjectName,
        appDir: '/data/apps/rbm_server',
        artifact: 'console-webapp/target/xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
        artifactName: 'xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
        scriptPath: 'jenkins/scripts/*',
        scriptLocalDir: '/data/jenkins/rbm/scripts',
        libDirInJenkins: 'jenkins/lib',
        libDirInServer: '/data/lib',
        aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
        env: 'pilotrun',
        port: 8080,
        tempServerPort: 8180
    ],
    'mdm-test-console': [
            buildProjectName: buildProjectName,
            appDir          : '/data/apps/mdm',
            artifact: 'console-webapp/target/xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
            artifactName: 'xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
            logDir         : '/data/logs',
            scriptPath      : 'jenkins/scripts/*',
            scriptLocalDir  : '/data/jenkins/mdm/scripts',
            libDirInJenkins: 'jenkins/lib',
            libDirInServer: '/data/lib',
            aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
            env: 'test',
            port: 8080,
            tempServerPort: 8180
    ],
    'mdm-pilotrun-console': [
        buildProjectName: buildProjectName,
        appDir          : '/data/apps/mdm_pilot',
        artifact: 'console-webapp/target/xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
        artifactName: 'xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
        logDir         : '/data/logs',
        scriptPath      : 'jenkins/scripts/*',
        scriptLocalDir  : '/data/jenkins/mdm/scripts',
        libDirInJenkins: 'jenkins/lib',
        libDirInServer: '/data/lib',
        aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
        env: 'pilotrun',
        port: 8080,
        tempServerPort: 8180
    ],
    'mdm-product-console'    : [
        buildProjectName: buildProjectName,
        appDir          : '/data/apps/mdm_pilot',
        artifact: 'console-webapp/target/xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
        artifactName: 'xkw-mdm-console-webapp-1.0-SNAPSHOT.jar',
        logDir         : '/data/logs',
        scriptPath      : 'jenkins/scripts/*',
        scriptLocalDir  : '/data/jenkins/mdm/scripts',
        libDirInJenkins: 'jenkins/lib',
        libDirInServer: '/data/lib',
        aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
        env: 'product',
        port: 8080,
        tempServerPort: 8180
    ],
    'mdm-test-api': [
            buildProjectName: buildProjectName,
            appDir          : '/data/apps/mds',
            artifact: 'api-webapp/target/xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            artifactName: 'xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            logDir         : '/data/logs',
            scriptPath      : 'jenkins/scripts/*',
            scriptLocalDir  : '/data/jenkins/mdm/scripts',
            libDirInJenkins: 'jenkins/lib',
            libDirInServer: '/data/lib',
            aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
            env: 'test',
            port: 8081,
            tempServerPort: 8181
    ],
    'mdm-test-gateway': [
            buildProjectName: buildProjectName,
            appDir          : '/data/apps/mdm_gateway',
            artifact: 'mdm-gateway/target/mdm-gateway-1.0-SNAPSHOT.jar',
            artifactName: 'mdm-gateway-1.0-SNAPSHOT.jar',
            logDir         : '/data/logs',
            scriptPath      : 'jenkins/scripts/*',
            scriptLocalDir  : '/data/jenkins/mdm/scripts',
            libDirInJenkins: 'jenkins/lib',
            libDirInServer: '/data/lib',
            aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
            env: 'test',
            port: 8101,
            tempServerPort: 8201
    ],
    'mdm-pilotrun-api': [
            buildProjectName: buildProjectName,
            appDir          : '/data/apps/mds_pilot',
            artifact: 'api-webapp/target/xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            artifactName: 'xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            logDir         : '/data/logs',
            scriptPath      : 'jenkins/scripts/*',
            scriptLocalDir  : '/data/jenkins/mdm/scripts',
            libDirInJenkins: 'jenkins/lib',
            libDirInServer: '/data/lib',
            aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
            env: 'pilotrun',
            port: 8081,
            tempServerPort: 8181
    ],
    'mdm-product-console'    : [
            buildProjectName: buildProjectName,
            appDir          : '/data/apps/mds',
            artifact: 'api-webapp/target/xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            artifactName: 'xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            logDir         : '/data/logs',
            scriptPath      : 'jenkins/scripts/*',
            scriptLocalDir  : '/data/jenkins/mdm/scripts',
            libDirInJenkins: 'jenkins/lib',
            libDirInServer: '/data/lib',
            aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
            env: 'product',
            port: 8081,
            tempServerPort: 8181
    ],
    'mdm-product-multi-node-console'    : [
            buildProjectName: buildProjectName,
            appDir          : '/data/apps/mds',
            artifact: 'api-webapp/target/xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            artifactName: 'xkw-mdm-api-webapp-1.0-SNAPSHOT.jar',
            logDir         : '/data/logs',
            scriptPath      : 'jenkins/scripts/*',
            scriptLocalDir  : '/data/jenkins/mdm/scripts',
            libDirInJenkins: 'jenkins/lib',
            libDirInServer: '/data/lib',
            aspectjweaverJarName: 'aspectjweaver-1.9.5.jar',
            env: 'product',
            port: 8081,
            tempServerPort: 8181
    ],
]

println "projectName $projectName"
def envProp = allEnvProps[projectName]

println(nodeName)
node(nodeName) {
//    checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: 'jcyy_svn_credential', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'http://114.55.64.147/svn/rbm/trunk/jenkins/deploy-scripts']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
//    checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: 'jcyy_svn_credential', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'http://114.55.64.147/svn/mdm/trunk']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: 'https://gitee.com/bjwangwei/gitee-test.git']]])
    def deploy = load 'jenkins/deploy-scripts/springbootServerDeploy.groovy'
    deploy.deploy(envProp)
}